<?php
include './_logger.php';
session_start();

if (isset($_SESSION["username"])) {
    header('Location: my-places.php');
} else {
    $isLogging = isset($_POST["username"]) && isset($_POST["password"]);

    if ($isLogging) {
        $username = $_POST["username"];
        $password = $_POST["password"];
        if (strtolower($username) == "infs" && $password == "3202") {
            $timeout = $_POST["timeout"];
            $_SESSION["username"] = $username;
            $_SESSION["login_time"] = time();
            $_SESSION["timeout"] = $timeout;
            writeLog("$username login");
            header('Location: my-places.php');
            exit;
        } else {
            $errorMessage = "Incorrect username/password";
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Login</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="css/prac1.css" type="text/css" rel="stylesheet">
    </head>
    <body>
        <h1>User Login</h1>
        <?= isset($errorMessage) ? $errorMessage : "" ?>
        <form action="login.php" method="post" <?= isset($errorMessage) ? 'class = "incorrect"' : "" ?>>
            <div class="user-credentials">
                <label for="username">Username</label>
                <input type="text" name="username">
            </div>

            <div class="user-credentials">
                <label for="password">Password</label>
                <input type="password" name="password">
            </div>

            <div>
                <label for="login-time">Stay logged in for </label>

                <select name="timeout">
                    <option value="30">30 sec</option>
                    <option value="60">1 min</option>
                </select>
            </div>

            <div>
                <input type="submit" name="login" value="Login">
            </div>
        </form>

    </body>
</html>
